package com.ww.demo;

import lombok.Data;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public class MethodHandleTest {
    public static void main(String[] args) {
        Student student = new Student();
        student.setAge(20);
        student.setName("张三");

        try {
            MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(
                    Student.class, MethodHandles.lookup());
            MethodHandle mh = lookup.findGetter(
                    Student.class, "name", String.class);
            String value = (String) mh.invokeExact(student);
            System.out.println(value);


//            MethodHandles.Lookup lookup = MethodHandles.lookup();
//            MethodHandle mh = lookup.findVirtual(Student.class, "getField", MethodType.methodType(FieldType.class));
//            Object value = mh.invoke(targetObj);

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

@Data
class Student {
    private String name;
    private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}